from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, Field, ConfigDict


# ========== 比赛相关模型 ==========

class CompetitionBase(BaseModel):
    """
    比赛基础模型
    包含比赛的基本信息字段
    """
    name: Optional[str] = Field(None, description="比赛名称")
    type: Optional[str] = Field(None, description="比赛类型（图片，视频）")
    competition_rule: Optional[str] = Field(None, description="比赛规则")
    description: Optional[str] = Field(None, description="比赛描述")
    content: Optional[str] = Field(None, description="比赛内容")
    cover: Optional[str] = Field(None, description="比赛封面")
    competition_number: Optional[int] = Field(50, description="参赛数量")
    competition_time: Optional[datetime] = Field(None, description="比赛时间")
    competition_end_time: Optional[datetime] = Field(None, description="比赛结束时间")
    judge_number: Optional[int] = Field(10, description="评委数量")
    judge_ids: Optional[str] = Field(None, description="评委id")
    competition_status: Optional[int] = Field(1, description="比赛状态 1-正常 2-禁用")
    competition_award: Optional[str] = Field(None, description="比赛奖项")


class CompetitionCreate(CompetitionBase):
    """
    创建比赛模型
    用于创建新比赛时的数据验证
    """
    name: str = Field(..., description="比赛名称")
    type: str = Field(..., description="比赛类型（图片，视频）")
    competition_time: datetime = Field(..., description="比赛时间")
    competition_end_time: datetime = Field(..., description="比赛结束时间")


class CompetitionUpdate(CompetitionBase):
    """
    更新比赛模型
    用于更新比赛信息时的数据验证
    """
    pass


class CompetitionResponse(CompetitionBase):
    """
    比赛响应模型
    用于API返回比赛信息
    """
    id: int = Field(..., description="比赛ID")
    create_time: datetime = Field(..., description="创建时间")
    update_time: datetime = Field(..., description="更新时间")
    delete_time: Optional[datetime] = Field(None, description="删除时间")

    model_config = ConfigDict(from_attributes=True)


class CompetitionListResponse(BaseModel):
    """
    比赛列表响应模型
    用于返回比赛列表数据
    """
    items: List[CompetitionResponse] = Field(..., description="比赛列表")
    total: int = Field(..., description="总数量")
    page: int = Field(..., description="当前页码")
    size: int = Field(..., description="每页数量")
    pages: int = Field(..., description="总页数")


class CompetitionQuery(BaseModel):
    """
    比赛查询模型
    用于比赛列表查询参数
    """
    page: int = Field(1, ge=1, description="页码")
    size: int = Field(10, ge=1, le=100, description="每页数量")
    name: Optional[str] = Field(None, description="比赛名称搜索")
    type: Optional[str] = Field(None, description="比赛类型筛选")
    status: Optional[int] = Field(None, description="比赛状态筛选")
    sort_by: Optional[str] = Field("create_time", description="排序字段")
    sort_order: Optional[str] = Field("desc", description="排序方式 asc/desc")


# ========== 参赛作品相关模型 ==========

class CompetitionWorkBase(BaseModel):
    """
    作品基础模型
    包含作品的基本信息字段
    """
    competition_id: Optional[int] = Field(None, description="比赛id")
    user_id: Optional[int] = Field(None, description="用户id")
    work_name: Optional[str] = Field(None, description="作品名称")
    work_description: Optional[str] = Field(None, description="作品描述")
    work_file: Optional[str] = Field(None, description="作品文件")
    work_type: Optional[str] = Field(None, description="作品类型（图片，视频）")
    work_cover: Optional[str] = Field(None, description="作品封面")
    work_status: Optional[int] = Field(2, description="作品状态 1-参赛 2-未参赛")
    work_audit_status: Optional[int] = Field(0, description="作品审核状态 1-审核中 2-审核通过 3-审核不通过")
    work_sort: Optional[int] = Field(0, description="作品排序")
    isPinned: Optional[bool] = Field(False, description="是否置顶")
    
    # 社交媒体相关字段
    xhs_url: Optional[str] = Field(None, description="小红书链接")
    dy_url: Optional[str] = Field(None, description="抖音链接")
    wb_url: Optional[str] = Field(None, description="微博链接")
    sph_url: Optional[str] = Field(None, description="视频号链接")

    xhs_like: Optional[int] = Field(0, description="小红书点赞")
    dy_like: Optional[int] = Field(0, description="抖音点赞")
    wb_like: Optional[int] = Field(0, description="微博点赞")
    sph_like: Optional[int] = Field(0, description="视频号点赞")

    xhs_comment: Optional[int] = Field(0, description="小红书评论")
    dy_comment: Optional[int] = Field(0, description="抖音评论")
    wb_comment: Optional[int] = Field(0, description="微博评论")
    sph_comment: Optional[int] = Field(0, description="视频号评论")

    wb_zf: Optional[int] = Field(0, description="微博转发")
    sph_collect: Optional[int] = Field(0, description="视频号收藏")
    xhs_collect: Optional[int] = Field(0, description="小红书收藏")
    dy_collect: Optional[int] = Field(0, description="抖音收藏")


    
    # 作品统计字段
    work_like: Optional[int] = Field(0, description="作品点赞")
    work_comment: Optional[int] = Field(0, description="作品评论")
    work_score: Optional[int] = Field(0, description="作品评分")
    work_rank: Optional[int] = Field(0, description="作品排名")
    work_award: Optional[str] = Field(None, description="作品奖项")
    work_label: Optional[str] = Field(None, description="作品标签")
    
    release_status: Optional[int] = Field(0, description="发布状态 0-草稿 1-提交作品 2-存为草稿")


class CompetitionWorkCreate(CompetitionWorkBase):
    """
    创建参赛作品模型
    用于创建作品时的数据验证
    注意：user_id通过JWT token自动获取，无需在请求中传递
    """
    competition_id: Optional[int] = Field(None, description="比赛id")
    work_name: str = Field(..., description="作品名称")
    work_file: str = Field(..., description="作品文件")
    work_type: str = Field(..., description="作品类型（图片，视频）")
    
    # user_id字段已移除，通过JWT token自动获取


class CompetitionWorkUpdate(CompetitionWorkBase):
    """
    更新参赛作品模型
    用于更新作品信息时的数据验证
    """
    pass


class CompetitionWorkResponse(CompetitionWorkBase):
    """
    参赛作品响应模型
    用于API返回作品信息
    """
    id: int = Field(..., description="作品ID")
    create_time: datetime = Field(..., description="创建时间")
    update_time: datetime = Field(..., description="更新时间")
    delete_time: Optional[datetime] = Field(None, description="删除时间")

    model_config = ConfigDict(from_attributes=True)


class CompetitionWorkListResponse(BaseModel):
    """
    参赛作品列表响应模型
    用于返回作品列表数据
    """
    items: List[CompetitionWorkResponse] = Field(..., description="作品列表")
    total: int = Field(..., description="总数量")
    page: int = Field(..., description="当前页码")
    size: int = Field(..., description="每页数量")
    pages: int = Field(..., description="总页数")


class CompetitionWorkQuery(BaseModel):
    """
    参赛作品查询模型
    用于作品列表查询参数
    """
    page: int = Field(1, ge=1, description="页码")
    size: int = Field(10, ge=1, le=100, description="每页数量")
    competition_id: Optional[int] = Field(None, description="比赛ID筛选")
    user_id: Optional[int] = Field(None, description="用户ID筛选")
    work_name: Optional[str] = Field(None, description="作品名称搜索")
    work_type: Optional[str] = Field(None, description="作品类型筛选")
    work_status: Optional[int] = Field(None, description="作品状态筛选")
    work_audit_status: Optional[int] = Field(None, description="审核状态筛选")
    sort_by: Optional[str] = Field("create_time", description="排序字段")
    sort_order: Optional[str] = Field("desc", description="排序方式 asc/desc")


# ========== 其他相关模型 ==========

class WorkScoreRequest(BaseModel):
    """
    作品评分请求模型
    用于评委给作品评分
    """
    work_id: int = Field(..., description="作品ID")
    score: int = Field(..., ge=0, le=100, description="评分 0-100")
    judge_id: int = Field(..., description="评委ID")


class WorkLikeRequest(BaseModel):
    """
    作品点赞请求模型
    用于用户给作品点赞
    """
    work_id: int = Field(..., description="作品ID")


class CompetitionStatusRequest(BaseModel):
    """
    比赛状态更新请求模型
    用于更新比赛状态
    """
    competition_id: int = Field(..., description="比赛ID")
    status: int = Field(..., description="比赛状态 1-正常 2-禁用")


class WorkAuditRequest(BaseModel):
    """
    作品审核请求模型
    用于作品审核操作的数据验证
    """
    work_id: int = Field(..., description="作品ID")
    audit_status: int = Field(..., description="审核状态 1-审核中 2-审核通过 3-审核不通过")
    audit_reason: Optional[str] = Field(None, description="审核原因")


class WorkSortRequest(BaseModel):
    """
    作品排序请求模型
    用于修改作品排序值的数据验证
    """
    work_id: int = Field(..., description="作品ID")
    work_sort: int = Field(..., ge=0, description="排序值，数值越大越靠前")


class WorkSortResponse(BaseModel):
    """
    作品排序响应模型
    用于返回排序操作的结果
    """
    code: int = Field(..., description="响应状态码")
    msg: str = Field(..., description="响应消息")
    data: Optional[dict] = Field(None, description="响应数据")


# ========== 评论相关模型 ==========

class WorkCommentBase(BaseModel):
    """
    作品评论基础模型
    包含评论的基本信息字段
    """
    work_id: Optional[int] = Field(None, description="关联作品ID")
    user_id: Optional[int] = Field(None, description="评论用户ID")
    parent_id: Optional[int] = Field(None, description="父评论ID，为空表示顶级评论")
    content: Optional[str] = Field(None, description="评论内容")
    like_count: Optional[int] = Field(0, description="点赞数")
    reply_count: Optional[int] = Field(0, description="回复数")


class WorkCommentCreate(BaseModel):
    """
    创建评论模型
    用于创建新评论时的数据验证
    只包含用户需要传递的必要字段
    """
    work_id: int = Field(..., description="关联作品ID")
    content: str = Field(..., min_length=1, max_length=1000, description="评论内容")
    parent_id: Optional[int] = Field(None, description="父评论ID，为空表示顶级评论")


class WorkCommentUpdate(BaseModel):
    """
    更新评论模型
    用于更新评论内容时的数据验证
    """
    content: str = Field(..., min_length=1, max_length=1000, description="评论内容")


class WorkCommentResponse(WorkCommentBase):
    """
    评论响应模型
    用于API返回评论信息
    """
    id: int = Field(..., description="评论ID")
    user_nickname: Optional[str] = Field(None, description="用户昵称")
    user_avatar: Optional[str] = Field(None, description="用户头像")
    reply_name: Optional[str] = Field(None, description="回复人昵称")
    is_delete: bool = Field(False, description="删除状态")
    is_liked: bool = Field(False, description="当前用户是否已点赞")
    create_time: datetime = Field(..., description="创建时间")
    update_time: datetime = Field(..., description="更新时间")
    delete_time: Optional[datetime] = Field(None, description="删除时间")
    replies: Optional[List['WorkCommentResponse']] = Field([], description="子评论列表")

    class Config:
        from_attributes = True


class WorkCommentListResponse(BaseModel):
    """
    评论列表响应模型
    用于返回分页的评论列表
    """
    items: List[WorkCommentResponse] = Field(..., description="评论列表")
    total: int = Field(..., description="总数量")
    page: int = Field(..., description="当前页码")
    size: int = Field(..., description="每页数量")
    pages: int = Field(..., description="总页数")


class WorkCommentQuery(BaseModel):
    """
    评论查询模型
    用于评论列表查询参数
    """
    page: int = Field(1, ge=1, description="页码")
    size: int = Field(10, ge=1, le=100, description="每页数量")
    work_id: int = Field(..., description="作品ID")
    parent_id: Optional[int] = Field(None, description="父评论ID，为空查询顶级评论")
    sort_by: Optional[str] = Field("create_time", description="排序字段")
    sort_order: Optional[str] = Field("desc", description="排序方式 asc/desc")


class CommentLikeRequest(BaseModel):
    """
    评论点赞请求模型
    用于评论点赞操作
    """
    comment_id: int = Field(..., description="评论ID")
    is_like: bool = Field(..., description="点赞状态：True-点赞，False-取消点赞")


class CommentDeleteRequest(BaseModel):
    """
    删除评论请求模型
    用于删除评论操作
    """
    comment_id: int = Field(..., description="评论ID")


class CommentResponse(BaseModel):
    """
    通用评论操作响应模型
    用于返回评论操作的结果
    """
    code: int = Field(..., description="响应状态码")
    msg: str = Field(..., description="响应消息")
    data: Optional[dict] = Field(None, description="响应数据")


class WorkCommentListUnifiedResponse(BaseModel):
    """
    评论列表统一响应模型
    用于返回统一格式的评论列表数据 (code, msg, data)
    """
    code: int = Field(..., description="响应状态码")
    msg: str = Field(..., description="响应消息")
    data: Optional[WorkCommentListResponse] = Field(None, description="评论列表数据")